﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMSSyncData.Model.WMSModel;
using WMSSyncData.Utils;

namespace WMSSyncData.DAL.MESTOWMS
{
    public class InStockDAL : BaseSqlDBDAL<WMS_T_InStock>
    {
        public List<WMS_T_InStock> GetInStock()
        {
            var sql = @"select newid() as Guid, BillNo,RelatedBillNo as RelatedBill,BillType,'0' as Status,Remark,Ext1,Ext2,Ext3,Creator,CreateTime,InWarehouse as Storage_SN  from wms_M_Instock where Data_Status=1";

            var ds = SqlDBHelper.GetDataSet(sql);

            var resutl = MakeTablePackage(ds.Tables[0]);

            return resutl;
        }

        public List<WMS_T_InStock> GetUpdateData()
        {
            var sql = @"SELECT a.BillNo FROM dbo.wms_T_InStock a
                        LEFT JOIN dbo.wms_T_InStockRows b ON a.BillNo=b.BillNo
                        LEFT JOIN dbo.wms_T_InStockRowDetails c ON b.BillNo=c.BillNo 
                        WHERE a.Status='1' AND a.BillType<>'返工入库单'  GROUP BY a.Storage_SN,a.BillNo,c.RelatedBill,c.PartNumber";

            var dt = SqlDBHelper.GetDataSet(sql).Tables[0];
            var result = MakeTablePackage(dt);
            return result;
        }

        public bool UpdateDataStatus(List<WMS_T_InStock> list)
        {
            var result = false;
            var billNoList = list.Select(x => x.BillNo).Distinct();
            if (billNoList.Count() > 0)
            {
                var sqlWhere = "where BillNo in (";
                foreach (var item in billNoList)
                {
                    sqlWhere = sqlWhere + "'" + item + "',";
                }
                sqlWhere.TrimEnd(',');
                sqlWhere = sqlWhere + ")";
                var sql = "UPDATE  wms_M_Instock SET Data_Status='2' " + sqlWhere;
                var updateRes = SqlDBHelper.ExcuteSQL(sql);
                if (updateRes > 0)
                {
                    result = true;
                }
            }
            return result;

        }
    }
}
